//
//  ExamineProprietorPositionView.swift
//  SmartHousekeeper
//
//  Created by 张磊 on 2020/2/21.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit


//身份验证页面
class ExamineProprietorPositionView: UIView {
    
    typealias ExamineProprietorPositionViewNextBlock = ()->()
    public var nextBlock : ExamineProprietorPositionViewNextBlock? = nil
    
    
    lazy var nextBtn : UIButton = {
        let value = UIButton(textColor: .white, textFont: UIFont.boldSystemFont(ofSize: Adapter(17)))
        value.setTitle("下一步", for: .normal)
        value.backgroundColor = RGBColor(r: 62, g: 134, b: 230, a: 1)
        value.layer.cornerRadius = Adapter(8)
        value.layer.masksToBounds = true
        return value
    }()
    
    lazy var nameText : UITextField = {
        let value = UITextField()
        value.placeholder = "请输入姓名"
        value.font = UIFont.boldSystemFont(ofSize: 15)
        value.textColor = RGBColor(r: 103, g: 103, b: 103, a: 1)
        value.textAlignment = .center
        value.delegate = self
        value.layer.cornerRadius = Adapter(8)
        value.layer.masksToBounds = true
        value.layer.borderWidth = 1
        value.layer.borderColor = RGBColor(r: 116, g: 116, b: 116, a: 1).cgColor
        return value
    }()
    
    lazy var idCardText : UITextField = {
        let value = UITextField()
        value.placeholder = "请输入身份证号"
        value.font = UIFont.boldSystemFont(ofSize: 15)
        value.textColor = RGBColor(r: 103, g: 103, b: 103, a: 1)
        value.textAlignment = .center
        value.delegate = self
        value.layer.cornerRadius = Adapter(8)
        value.layer.masksToBounds = true
        value.layer.borderWidth = 1
        value.layer.borderColor = RGBColor(r: 116, g: 116, b: 116, a: 1).cgColor
        return value
    }()
    
    lazy var iphoneText : UITextField = {
        let value = UITextField()
        value.placeholder = "请输入手机号"
        value.font = UIFont.boldSystemFont(ofSize: 15)
        value.textColor = RGBColor(r: 103, g: 103, b: 103, a: 1)
        value.textAlignment = .center
        value.delegate = self 
        value.layer.cornerRadius = Adapter(8)
        value.layer.masksToBounds = true
        value.layer.borderWidth = 1
        value.layer.borderColor = RGBColor(r: 116, g: 116, b: 116, a: 1).cgColor
        value.keyboardType = .numberPad
        return value
    }()
    
    
    required init?(coder: NSCoder) {
        super .init(coder: coder)
    }
    override init(frame: CGRect) {
        super .init(frame: frame)
        setUI()
    }
    private func setUI(){
        
        //业主姓名(与房产证匹配)
        let nameL = UILabel(textColor: RGBColor(r: 116, g: 116, b: 116, a: 1), textFont: UIFont.systemFont(ofSize: 13))
        nameL.text = "业主姓名(与房产证匹配):"
        self.addSubview(nameL)
        nameL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(22))
            make.top.equalToSuperview().offset(Adapter(22))
            make.height.equalTo(Adapter(15))
        }
        
        self.addSubview(nameText)
        nameText.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(22))
            make.right.equalToSuperview().offset(Adapter(-22))
            make.top.equalTo(nameL.snp.bottom).offset(Adapter(10))
            make.height.equalTo(Adapter(45))
        }
        
        //身份证号码
        let idcardL = UILabel(textColor: RGBColor(r: 116, g: 116, b: 116, a: 1), textFont: UIFont.systemFont(ofSize: 13))
        idcardL.text = "身份证号码:"
        self.addSubview(idcardL)
        idcardL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(22))
            make.top.equalTo(nameText.snp.bottom).offset(Adapter(25))
            make.height.equalTo(Adapter(15))
        }
        
        self.addSubview(idCardText)
        idCardText.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(22))
            make.right.equalToSuperview().offset(Adapter(-22))
            make.top.equalTo(idcardL.snp.bottom).offset(Adapter(10))
            make.height.equalTo(Adapter(45))
        }
        
        //手机号码
        let iphoneL = UILabel(textColor: RGBColor(r: 116, g: 116, b: 116, a: 1), textFont: UIFont.systemFont(ofSize: 13))
        iphoneL.text = "手机号码:"
        self.addSubview(iphoneL)
        iphoneL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(22))
            make.top.equalTo(idCardText.snp.bottom).offset(Adapter(25))
            make.height.equalTo(Adapter(15))
        }
        
        self.addSubview(iphoneText)
        iphoneText.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(Adapter(22))
            make.right.equalToSuperview().offset(Adapter(-22))
            make.top.equalTo(iphoneL.snp.bottom).offset(Adapter(10))
            make.height.equalTo(Adapter(45))
        }
        
        
        self.addSubview(nextBtn)
        nextBtn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(Adapter(-50))
            make.height.equalTo(Adapter(45))
            make.left.equalToSuperview().offset(Adapter(50))
            make.right.equalToSuperview().offset(Adapter(-50))
        }
        
        nextBtn.sce.add(.touchUpInside) { (btn) in
            //            if self.nameText.text!.isEmpty {
            //               print("请输入姓名")
            //            } else if self.idCardText.text!.isEmpty {
            //                print("请输入身份证")
            //            } else if !isIDCard(idCard: self.idCardText.text!) {
            //                print("身份证格式不正确")
            //            } else if self.iphoneText.text!.isEmpty {
            //                print("请输入手机号")
            //            } else if !isChinaPhone(str: self.iphoneText.text!) {
            //                print("手机号格式不正确")
            //            } else {
            if self.nextBlock != nil {
                self.nextBlock!()
            }
            //            }
        }
        
    }
    
   
    
}
extension ExamineProprietorPositionView : UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        switch textField {
        case nameText:
            if nameText.text!.isEmpty {
                print("请输入姓名")
            }
            break
        case idCardText:
            if idCardText.text!.isEmpty {
                print("请输入身份证")
            } else if !isIDCard(idCard: idCardText.text!) {
                print("身份证格式不正确")
            }
            break
        case iphoneText:
            if iphoneText.text!.isEmpty {
                print("请输入手机号")
            } else if !isChinaPhone(str: iphoneText.text!) {
                print("手机号格式不正确")
            }
            
            break
            
        default: break
            
        }
    }
    //    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //
    //    }
}
